﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections;

public partial class goodskid : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["name"] == null)
        {
            Response.Redirect("~/error.aspx?");
        }
        if (Session["power"].ToString() == "查询用户")
        {
            Button1.Visible = false;
            Button2.Visible = false;
        }

        string JudgePower = Session["power"].ToString();
        if (JudgePower != "超级管理员" && JudgePower != "仓库管理员" && JudgePower != "销售" && JudgePower != "主管")
        {

            Response.Redirect("~/PowerError.aspx?");
        }
            if (!IsPostBack)
            {
                this.bind();
            }
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/addgoodskid.aspx?");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (GridView1.SelectedValue != null)
        {
            SqlConnection myConn = GetConnection();
            myConn.Open();
            string sqlStr = "delete from [Categories] where CID='" + GridView1.SelectedValue.ToString() + "'";
            SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
            myCmd.ExecuteNonQuery();
            myConn.Close();
            //Response.Write("<script>alert('删除成功!')</script>");
            Response.Redirect("~/goodskid.aspx?");
        }
        else
            Response.Write("<script>alert('未选择要删除的选项!')</script>");
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        Response.AddHeader("Refresh", "0"); 
    }

    public SqlConnection GetConnection()
    {
        string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
        SqlConnection myConn = new SqlConnection(myStr);
        return myConn;
    }

    protected void bind()
    {
        SqlConnection myConn = GetConnection();
        myConn.Open();
        string sqlStr = "select CID as 类别编号,CName as 类别名称 from [Categories]";
        SqlDataAdapter myDa = new SqlDataAdapter(sqlStr, myConn);
        DataSet myDs = new DataSet();
        myDa.Fill(myDs);
        GridView1.DataSource = myDs;
        GridView1.DataKeyNames = new string[] { "类别编号" };
        GridView1.DataBind();
        myDa.Dispose();
        myDs.Dispose();

        myConn.Close();
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
    }
}